package com.csw.mysqldate.controller;

import com.csw.mysqldate.entity.SortEntity;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
public class SortTest {

	public static void main(String[] args) {
		List<SortEntity> sortEntityList = new ArrayList<>();

		SortEntity sortEntity1 = new SortEntity();
		sortEntity1.setId(1);
		sortEntity1.setRatio("34.23");
		sortEntity1.setAge(1);
		sortEntity1.setPercentage("34.23%");
		sortEntityList.add(sortEntity1);


		SortEntity sortEntity2 = new SortEntity();
		sortEntity2.setId(2);
		sortEntity2.setRatio("33.23");
		sortEntity2.setAge(3);
		sortEntity2.setPercentage("33.24%");
		sortEntityList.add(sortEntity2);


		SortEntity sortEntity3 = new SortEntity();
		sortEntity3.setId(3);
		sortEntity3.setRatio("33.23");
		sortEntity3.setAge(2);
		sortEntity3.setPercentage("33.33%");
		sortEntityList.add(sortEntity3);

		SortEntity sortEntity4 = new SortEntity();
		sortEntity4.setId(4);
		sortEntity4.setRatio("35.23");
		sortEntity4.setAge(3);
		sortEntity4.setPercentage("35.23%");
		sortEntityList.add(sortEntity4);

		SortEntity sortEntity5 = new SortEntity();
		sortEntity5.setId(5);
		sortEntity5.setRatio("36.23");
		sortEntity5.setAge(5);
		sortEntity5.setPercentage("36.23%");
		sortEntityList.add(sortEntity5);
		log.info("" + "//原始//////////////////排序前");
		for (SortEntity sortEntity : sortEntityList) {
			log.info("" + sortEntity);
		}

		//【最新方式】
//按照时间字段降序排序
		//relatedRecommendationVos.sort(Comparator.comparing(RelatedRecommendationVo::getReleaseTime).reversed());
		//多字段
//		resList.sort(Comparator.comparing(UrbanPartyBuildColumnListPcVo::getLevel).thenComparing(UrbanPartyBuildColumnListPcVo::getParentId).thenComparing(UrbanPartyBuildColumnListPcVo::getSequence));
		/**
		 * java里面写比较规则
		 */
		sortEntityList.sort((Comparator) (o1, o2) -> {
			SortEntity stu1 = (SortEntity) o1;
			SortEntity stu2 = (SortEntity) o2;
			if (!stu2.getRatio().equals(stu1.getRatio())) {
				return stu2.getRatio().compareTo(stu1.getRatio());
			} else if (!stu2.getPercentage().equals(stu1.getPercentage())) {
				return stu1.getPercentage().compareTo(stu2.getPercentage());
			} else {
				return (stu2.getId() + "").compareTo(stu1.getId() + "");
			}
		});
		log.info("" + "//String类型排序////////////////////排序后");
		for (SortEntity sortEntity : sortEntityList) {
			log.info("" + sortEntity);
		}
		/**
		 * 用实体里面的比较规则
		 */
		sortEntityList.sort(SortEntity::compareTo);
		log.info("" + "//实体int类型排序////////////////////排序后");
		for (SortEntity sortEntity : sortEntityList) {
			log.info("" + sortEntity);
		}
		/**
		 * java里面写比较规则
		 * 单条件
		 */
		sortEntityList.sort((Comparator) (o1, o2) -> {
			SortEntity stu1 = (SortEntity) o1;
			SortEntity stu2 = (SortEntity) o2;
			if (!stu2.getPercentage().equals(stu1.getPercentage())) {
				return stu1.getPercentage().compareTo(stu2.getPercentage());
			} else {
				return 1;
			}
		});
		log.info("" + "//实体per类型排序////////////////////排序后");
		for (SortEntity sortEntity : sortEntityList) {
            //sortEntity.getPercentage());
        }

		/**
		 * 如果这个类型的升序可以直接用工具类
		 *  List<Integer> indexes = new ArrayList<>();
		 *  Collections.sort(indexes);
		 */
	}

}
